package common.entities.nonpersistent;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;



/**
 * @author Yoyo
 *
 */
public class Game {
	
	private List<GameQuestion> questionsList;
	private List<GameQuestion> wheelQuestionsList;
	private int currQuestion = -1;
	private boolean helpWheelReplaceQuestion = true;
	private boolean helpWheelExtraTime = true;
	private boolean helpWheelFiftyFifty = true;
	private int level = 1;
	private int totalScore = 0;
	
	private static int[] questionsValue  =  new int[]{100, 200, 300, 500, 1000,
												2000, 4000, 8000, 10000, 32000,
												64000, 125000, 250000, 500000, 1000000};
	

	public final static int questionTime = 60; /*The time here is specified in seconds*/
	public final static int extraQuestionTime = 30;
	public final static int questionForGame = 15;
	public final static int questionForLevel = 5;
	
	public Game(List<GameQuestion> questionsList){
		
		wheelQuestionsList = new LinkedList<GameQuestion>();
		wheelQuestionsList.add(questionsList.remove(questionForLevel));
		wheelQuestionsList.add(questionsList.remove(questionForLevel*2));
		wheelQuestionsList.add(questionsList.remove(questionForLevel*3));
		this.questionsList = questionsList;
	}
	
	public boolean hasNextQuestion(){
		if (currQuestion+1<questionForGame) return true;
		return false;
	}
	public GameQuestion NextQuestion(){
		currQuestion++;
		
		//change the level
		if (currQuestion>questionForLevel){
			if (currQuestion<questionForLevel*2) level = 2;
			else level = 3;
		}
		
		GameQuestion question = questionsList.get(currQuestion);
		
		//set the right value to the question
		question.setQuestionValue(questionsValue[currQuestion]);
		return question;
	}
	public boolean submitAnswer(int answerNumber) {
		if (questionsList.get(currQuestion).getRightAnswerIndex() == answerNumber)
		{
			totalScore=questionsList.get(currQuestion).getQuestionValue();
			return true;
		}
			
		return false;
	}
	public GameQuestion helpWheelReplaceQuestion() {
		if (!helpWheelReplaceQuestion) return null;
		helpWheelReplaceQuestion = false;
		GameQuestion question;
		if (level == 1)  question = wheelQuestionsList.get(0);
		if (level == 2) question = wheelQuestionsList.get(1);
		else question = questionsList.get(2);
		question.setQuestionValue(questionsValue[currQuestion]);
		
		//replace the current question
		questionsList.remove(currQuestion);
		questionsList.add(currQuestion, question);
		
		//questionsList.
		return question;
	}
	
	public int[] helpWheelFiftyFifty(){
		if (!helpWheelFiftyFifty) return null;
		helpWheelFiftyFifty = false;
		int[] answers = new int[2];
		Random gen = new Random();
		int temp = gen.nextInt(2);
		
		if (temp == questionsList.get(currQuestion).getRightAnswerIndex())
			temp = (temp + 1) % 4;
		
		answers[0] = temp;
		
		temp = gen.nextInt(2);
		if (temp == questionsList.get(currQuestion).getRightAnswerIndex())
			temp = (temp + 1) % 4;
		
		if (temp == answers[0]){
			temp = (temp + 1) % 4;
		}
		answers[1] = temp;
		/*switch the answers to be in the right order*/
		if (answers[1]<answers[0]) {
			int temp2 = answers[0];
			answers[0] = answers[1];
			answers[1] = temp2;
		}
		return answers;
	}

	
	public int helpWheelExtraTime(){
		if (!helpWheelExtraTime) return 0;
		helpWheelExtraTime = false;
		return Game.extraQuestionTime;
	}
	
	public int getScore(){
		return totalScore;
	}
	
}
